package com.ssm.server.common;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import net.sf.json.JSONObject;




public class ResponseEntity<T> {
    
    private static Logger LOG = Logger.getLogger(ResponseEntity.class);
    
    private String STATUS;
    
    private String MSG;
    
    private Object OBJECT;
    
    private Collection<T> LIST;
    
    
    public static final String SUCCESS_STATUS = "0";// 服务器ok
    public static final String WARN_STATUS = "1";// 服务器响应正常,但不是客户端要的,警告warn
    public static final String ERROR_STATUS = "2";// 服务器出现error
    public static final String NO_LOGIN = "3";// 用户未登录
    public static final String RELOGIN_STATUS="4";
    public static final String VERIFYCODE_WARN_STATUS = "6";
    public static final String NOTIFY_STATUS = "7";
    public static final String GOOGLE_AUTH_STATUS = "8";
    public static final String GOOGLE_AUTH_NOTIFY_STATUS = "9";
    public ResponseEntity() {
        super();
    }
    
    public ResponseEntity(Object obj) {
        super();
        this.OBJECT = obj;
    }
    
	public ResponseEntity(List<T> list) {
		super();
		this.LIST = list;
	}
    
    public ResponseEntity(String sTATUS, String mSG) {
        super();
        STATUS = sTATUS;
        MSG = mSG;
    }
    
    public ResponseEntity(String sTATUS, String mSG, Object obj) {
        super();
        STATUS = sTATUS;
        MSG = mSG;
        this.OBJECT = obj;
    }
    
    public ResponseEntity(String sTATUS, String mSG, Collection<T> list) {
		super();
		STATUS = sTATUS;
		MSG = mSG;
		this.LIST = list;
	}

	public ResponseEntity(String sTATUS, String mSG, Object oBJECT, List<T> lIST) {
		super();
		STATUS = sTATUS;
		MSG = mSG;
		this.OBJECT = oBJECT;
		LIST = lIST;
	}
    
  
    
   /* public ResponseEntity(String sTATUS, String mSG, Collection<SearchCityEntity> lIST) {
        STATUS = sTATUS;
        MSG = mSG;
        LIST2 = lIST;
    }*/
    
    
   
    public String getSTATUS() {
        return STATUS;
    }
    
    public void setSTATUS(String sTATUS) {
        STATUS = sTATUS;
    }
    
    public String getMSG() {
        return MSG;
    }
    
    public void setMSG(String mSG) {
        MSG = mSG;
    }
    
    public Object getOBJECT() {
        return OBJECT;
    }
    
    public void setOBJECT(Object oBJECT) {
        OBJECT = oBJECT;
    }
   
    
    public Collection<T> getLIST() {
		return LIST;
	}

	public void setLIST(Collection<T> lIST) {
		LIST = lIST;
	}

	@Override
    public String toString() {
        return "ResponseEntity [STATUS=" + STATUS + ", MSG=" + MSG + "]";
    }

    
	/**
	 * @Title: println @Description: 向客户端打印出服务端的状态 @param @param
	 *         request @param @param STATUS @param @param
	 *         MSG @param @return @return String 返回类型 @throws
	 */
	public static void println(HttpServletResponse response, String STATUS, String MSG, String sessionId) {
		try {
			PrintWriter out = response.getWriter();
			String json = JSONObject.fromObject(new ResponseEntity<Object>(STATUS, MSG, sessionId)).toString();
			LOG.info(json);
			out.println(json);
			out.flush();
		} catch (IOException e) {
			LOG.error("从HttpServletResponse获取输出流出现异常");
			throw new RuntimeException(e);
		}
	}

	public static void println(HttpServletResponse response, String STATUS, String MSG) {
		try {
			PrintWriter out = response.getWriter();
			String json = JSONObject.fromObject(new ResponseEntity<Object>(STATUS, MSG)).toString();
			LOG.info(json);
			out.println(json);
			out.flush();
		} catch (IOException e) {
			LOG.error("从HttpServletResponse获取输出流出现异常");
			throw new RuntimeException(e);
		}
	}

    
    /**
     * @Title: println
     * @Description: 向客户端打印出服务端的状态
     * @param @param request
     * @param @param STATUS
     * @param @param MSG
     * @param @param obj
     * @param @return
     * @return String 返回类型
     * @throws
     */
    public static void println(HttpServletResponse response, String STATUS, String MSG, Object obj) {
        try {
            PrintWriter out = response.getWriter();
            String json = JSONObject.fromObject(new ResponseEntity<Object>(STATUS, MSG, obj)).toString();
            LOG.info(json);
            out.println(json);
            out.flush();
        }
        catch (IOException e) {
            LOG.error("从HttpServletResponse获取输出流出现异常");
            throw new RuntimeException(e);
        }
    }
    
    /**
     * @Title: println
     * @Description:向客户端返回SUCCESS状态
     * @param @param response
     * @return void 返回类型
     * @throws
     */
    public static void println(HttpServletResponse response) {
        try {
            PrintWriter out = response.getWriter();
            String json = JSONObject.fromObject(new ResponseEntity<Object>(SUCCESS_STATUS, "SUCCESS")).toString();
            LOG.info(json);
            out.println(json);
            out.flush();
        }
        catch (IOException e) {
            LOG.error("从HttpServletResponse获取输出流出现异常");
            throw new RuntimeException(e);
        }
    }
    
    /**
     * @Title: println
     * @Description: 将一个对象和状态码一起封装成json
     * @param @param response
     * @param @param obj
     * @return void 返回类型
     * @throws
     */
    public static void println(HttpServletResponse response, Object obj) {
        try {
            PrintWriter out = response.getWriter();
            JSONObject json = JSONObject.fromObject(new ResponseEntity<Object>(SUCCESS_STATUS, "SUCCESS", obj));
            LOG.info(json.toString());
            out.print(json.toString());
            out.flush();
        }
        catch (IOException e) {
            LOG.error("从HttpServletResponse获取输出流出现异常");
            throw new RuntimeException(e);
        }
    }
    
    /**
     * @Title: println
     * @Description: 将对象集合封装成json
     * @param @param response
     * @param @param list
     * @return void 返回类型
     * @throws
     */

	public static <T> void printlns(HttpServletResponse response, Collection<T> list) {
		try {
			PrintWriter out = response.getWriter();
			JSONObject json = JSONObject.fromObject(new ResponseEntity<T>(SUCCESS_STATUS, "SUCCESS", list));
			LOG.info(json.toString());
			out.println(json.toString());
			out.flush();
		} catch (IOException e) {
			LOG.error("从HttpServletResponse获取输出流出现异常");
			throw new RuntimeException(e);
		}
	}
    
    /**
     * @Title: println
     * @Description: 将对象封装到json中去
     * @param @param response
     * @param @param obj
     * @param @param list
     * @return void 返回类型
     * @throws
     */
	public static <T> void println(HttpServletResponse response, Object obj, List<T> list) {
		try {
			PrintWriter out = response.getWriter();
			JSONObject json = JSONObject.fromObject(new ResponseEntity<T>(SUCCESS_STATUS, "SUCCESS", obj, list));
			LOG.info(json.toString());
			out.println(json.toString());
			out.flush();
		} catch (IOException e) {
			LOG.error("从HttpServletResponse获取输出流出现异常");
			throw new RuntimeException(e);
		}
	}
    
    
	public static <T> void println(HttpServletResponse response, String mSG2, Object obj, List<T> list) {
		try {
			PrintWriter out = response.getWriter();
			JSONObject json = null;
			if ("".equals(mSG2)) {
				json = JSONObject.fromObject(new ResponseEntity<T>(SUCCESS_STATUS, mSG2, obj, list));
			} else {
				json = JSONObject.fromObject(new ResponseEntity<T>(WARN_STATUS, mSG2, obj, list));
			}
			LOG.info(json.toString());
			out.println(json.toString());
			out.flush();
		} catch (IOException e) {
			LOG.error("从HttpServletResponse获取输出流出现异常");
			throw new RuntimeException(e);
		}
	}
}
